﻿///////////////////////////////////////////////////////////////////////////////
//
//  This file is part of MathLib.NET.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//  
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//  
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library;  If not, see 
//  <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

using ILNumerics;
using ILNumerics.BuiltInFunctions;

// Interpolation functions
// =======================
//
// dsearch - Search Delaunay triangulation for nearest point
// dsearchn - N-D nearest point search
// griddata - Data gridding
// griddata3 - Data gridding and hypersurface fitting for 3-D data
// griddatan - Data gridding and hypersurface fitting (dimension ≥ 2)
// griddedInterpolant - Interpolant for gridded data
// interp1 - 1-D data interpolation (table lookup)
// interp1q - Quick 1-D linear interpolation
// interp2 - 2-D data interpolation (table lookup)
// interp3 - 3-D data interpolation (table lookup)
// interpft - 1-D interpolation using FFT method
// interpn - N-D data interpolation (table lookup)
// meshgrid - Rectangular grid in 2-D and 3-D space
// mkpp - Make piecewise polynomial
// ndgrid - Rectangular grid in N-D space
// padecoef - Padé approximation of time delays
// pchip - Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)
// ppval - Evaluate piecewise polynomial
// spline - Cubic spline data interpolation
// TriScatteredInterp - Interpolate scattered data
// tsearch - Search for enclosing Delaunay triangle
// tsearchn - N-D closest simplex search
// unmkpp - Piecewise polynomial details

namespace MathLib
{
    /// <summary>
    /// Interpolation functions.
    /// </summary>
    public partial class MLMath
    {
        public static ILArray<double> interp1(ILArray<double> x, ILArray<double> Y, ILArray<double> xi, string method = "linear")
        {
            if (Y.IsScalar)
            {
                // expand Y to size of x

                ILArray<double> yi = new ILArray<double>(xi.Dimensions.ToIntArray());

                // TODO: Implement interp1 for scalar Y

                return yi;
            }
            else if (Y.IsVector)
            {
                if (x.InternalArray4Experts.Length != Y.InternalArray4Experts.Length)
                    throw new MathException("interp1(): x must have the same size as vector Y.");

                ILArray<double> yi = new ILArray<double>(xi.Dimensions.ToIntArray());

                MathNet.Numerics.Interpolation.Algorithms.LinearSplineInterpolation lsi = new MathNet.Numerics.Interpolation.Algorithms.LinearSplineInterpolation();
                lsi.Initialize(x.InternalArray4Experts, Y.InternalArray4Experts);
                for (int i = 0; i < xi.InternalArray4Experts.Length; i++)
                    yi.InternalArray4Experts[i] = lsi.Interpolate(xi.InternalArray4Experts[i]);

                return yi;
            }
            else
            {
                if (x.InternalArray4Experts.Length != Y.Dimensions[0])
                    throw new MathException("interp1(): the size of Y must have the form [n,d1,d2,...,dk], where n is the length of x");

                // TODO: Implement interp1 for matrix Y
            }
            
            return null;
        }

        // TODO: Implement additional interp1 overloads
    }
}